﻿CREATE PROCEDURE [acms].[Template_Put]
	@Id int, 
	@SiteId int,
	@Name varchar(255),
	@ConcurencyId rowversion,
	@IdOut int output,
	@ConcurencyIdOut rowversion output
AS
	if Exists (Select null FROM Template_View where Id = @Id) begin
		update Templates
		set
			SiteId = @SiteId,
			Name = @Name
		where Id = @Id
			and ConcurencyId = @ConcurencyId
		
		if(@@RowCount = 0)
			return -100;
		else
			set @IdOut = @Id
	end
	else if @Id is null or @Id = 0 begin
			insert	Templates
			(SiteId, Name)
		values
			(@SiteId, @Name)
		set @IdOut = @@Identity
	end
	else begin
		return -200
	end
	select @ConcurencyIdOut = ConcurencyId From Templates where Id = @IdOut
RETURN 0;